import _ from "lodash";
// 设备运行中心的轮询间隔 5秒

export const DEVICE_TIME = 5000;
// 定时任务轮询间隔
export const TIME_TASK_TIME = 5000;

import { getMeasurements } from "@/api/assignments";
// 任务状态
export const TASK_STATUS = ["未下达", "已下达", "正在执行", "完成", "终止"];
// 告警中心轮询间隔 5秒
export const WARNING_TIME = 5000;
// 实时温度的颜色
export const LINE_COLOES = [
  "#eb2100",
  "#d0a00e",
  "#38e27f",
  "#00ADD0",
  "#FFA12F",
  "#B62AFF",
  "#604BFF",
  "#6E35FF",
  "#002AFF"
];
// 格式化展示的数据 (有些是数组,有些是字符串,这里单独处理)
export function formatPropVal(arr) {
  const isArr = Object.prototype.toString.call(arr);
  if (isArr == "[object Array]") {
    if (arr && arr.length) {
      return arr.join(",");
    } else {
      return "--";
    }
  } else {
    return arr || "--";
  }
}
export const useTable = (keys, data) => {
  if (!data.length) {
    return [];
  }
  const cloneData = _.cloneDeep(data);
  cloneData.forEach((item, index) => {
    let obj = {};
    item[keys[0]].forEach((it, i) => {
      keys.forEach(key => {
        if (!item[key]) {
          obj[key] = "";
        } else {
          const isArr = Object.prototype.toString.call(item[key]);
          obj[key] = isArr == "[object String]" ? item[key] : item[key][i];
        }
      });
    });
    if (item.list) {
      item.list.push(obj);
    } else {
      item.list = [obj];
    }
  });
  console.log("cloneData :>> ", cloneData);
  return cloneData;
};
// 找出对应节点
export function getTreeNode(list, deviceToken) {
  for (let i = 0; i < list.length; i++) {
    let a = list[i];
    if (a.type === 4 && a.token == deviceToken) {
      return a;
    } else {
      if (a.children && a.children.length > 0) {
        let res = getTreeNode(a.children, deviceToken);
        if (res) {
          return res;
        }
      }
    }
  }
}
// 找树结构的第一级的第一个
export function getChild(node) {
  for (let i = 0; i < node.length; i++) {
    let a = node[i];
    if (a.type === 4) {
      return a;
    } else {
      if (a.children && a.children.length > 0) {
        let res = getChild(a.children);
        if (res) {
          return res;
        }
      }
    }
  }
}
// 查找父节点
export function getAllParentNodes(list, id) {
  for (let i in list) {
    if (list[i].token === id) {
      return [list[i]].filter(v => v.token !== id);
    }
    if (list[i].children?.length > 0) {
      let node = getAllParentNodes(list[i].children, id);
      if (node) return node.concat(list[i]).filter(v => v.token !== id);
    }
  }
}

// 找到父节点的 name 常规热单元处理
export function parentTreeName(arr, token) {
  //arr 所有的树数据 id 某个子节点的id
  let name = "";
  arr.forEach(item1 => {
    item1.children.forEach(item2 => {
      item2.children.forEach(item3 => {
        if (item3.token == token) {
          name = item2.name;
        }
      });
    });
  });
  return name;
}
//计算百分比
export function roundPercentageTotals(num1, num2, num3) {
  var total = num1 + num2 + num3; // 117
  var num1Total = (num1 / total) * 100; // 14.529914529914531
  var num2Total = (num2 / total) * 100; //  8.547008547008546
  var num3Total = (num3 / total) * 100; // 76.92307692307693

  var num1ToDecimal = num1Total.toFixed(1); // 14.5
  var num2ToDecimal = num2Total.toFixed(1); //  8.5
  var num3ToDecimal = num3Total.toFixed(1); // 76.9

  var totalPercentage =
    parseInt(num1ToDecimal) + parseInt(num2ToDecimal) + parseInt(num3ToDecimal); // 98

  return {
    upLimitalarmCount: Math.round(num1ToDecimal),
    deviceWarmalarmCount: Math.round(num2ToDecimal),
    comWarmalarmCount: Math.round(num3ToDecimal)
  };
}
// 递归获取所有的表数据
export const getAssignmentsAllData = (assignmentToken, searchForm) => {
  return new Promise((resolve, reject) => {
    let arr = [];
    let page = 1;
    let pageSize = 3000;
    let numResult = -1;
    getData();
    async function getData() {
      if (numResult == arr.length) {
        resolve(arr);
        return;
      }
      const res = await getMeasurements(
        {
          page,
          pageSize: pageSize,
          ...searchForm
        },
        assignmentToken
      );

      page++;
      const { results, numResults } = res.data;
      numResult = numResults;
      arr = arr.concat(results);
      getData();
    }
  });
};
